from functools import wraps
import sys
import time
from termcolor import colored
def timeit(color='magenta',tag='',force=False,end='\n'):
    """装饰器：测量函数执行时间，并允许指定输出颜色"""
    def decorator(func):
        @wraps(func)
        def wrapper(*args,**kwargs):
            start_time = time.time()
            result = func(*args, **kwargs)
            end_time = time.time()
            # if (end_time - start_time)>0.1 or func.__name__ in ['fpsPcd']:
            if force:
                print(colored(f"{func.__name__} time:{(end_time - start_time):.3f}s {tag}", color),end=end,flush=True)
            return result
        return wrapper
    return decorator